<?php
global $_RQ,$_AR;
if($_SESSION['os']=='windows'){
	exi('','',SITEROOT.'/manage/index.php?action=user.login');
}
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['role']=$_SESSION['role'];
$setting=$this->plugin_setting('setting');
if(!$setting['price1'] || !$setting['start'] || !$setting['juli']){
	exi('未设置计费价格，请联系管理员设置','error','/home/');
}
$return['member']=array();
$this->checkmember();
$return['sharedata']=array(
		'title'=>$setting['share_title'],
		'desc'=>'我分享了一个打的应用，快来看看吧',
		'link'=>custom_url('jy_daijia/index/',true),
		'imgUrl'=>tomedia($setting['share_image']),
	);
if($_RQ['op']=='main'){
	$return['uid']=$_SESSION['uid'];
	$return['info']=array();
	$return['show']=0;
	$return['show2']=0;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);	
	$params=array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
    $orderby='createtime desc';
	$total='';
	$history=pdo_getslice('jy_daijia_history',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($history as &$v){
		$v['lat_lng']=json_decode($v['lat_lng'],true);
	}
	$return['history']=$history;
	if($_RQ['lat']){
		$return['info']['lat']=cfc('tools')->Convert_GCJ02_To_BD09($_RQ['lat'],$_RQ['lng']);
		
	}
	$return['params']=array();
	$return['nomarkers']=array(
			'title'=>'附近暂无服务人员，叫代驾请拨'.$return['pluginset']['phone'],
			'iconPath'=>$return['pluginset']['map_icon']?tomedia($return['pluginset']['map_icon']):tomedia('../addons/jy_daijia/resource/images/map_icon.png'),
			'width'=>36,
			'height'=>36,
			'callout'=>array(
				'content'=>'附近暂无服务人员，叫代驾请拨'.$return['pluginset']['phone'],
				'borderRadius'=>5,
				'bgColor'=>'#333',
				'color'=>'#fff',
				'textAlign'=>'center',
				'padding'=>5,
				'anchorY'=>0,
				'display'=>'ALWAYS',
				'borderColor'=>'#333',
			),
	);
	$return['isorder']=pdo_get('jy_daijia_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1,6,7,8)));
	$return['isorder']=$return['isorder']?:array();
	$is_xieyi=pdo_getcolumn('jy_daijia_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'is_xieyi');
	if(!$is_xieyi && $_SESSION['uid']){
		$return['xieyi']=$this->plugin_setting('daijia_xieyi')?:array();
		$return['xieyi']['title']='使用本代驾平台协议';
		$return['show_xieyi']=$return['xieyi']['content']?true:false;
	}
	//获取小程序订阅消息ID
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('1290',[1,2,3,4],'create2buyer','新订单通知','jy_daijia');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('205',[1,2,3],'get2user','代驾司机接单通知','jy_daijia');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('4968',[1,2,6,7,5],'pay2buyer','订单待支付通知','jy_daijia');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	exi($return);
}
if($_RQ['op']=='pirce'){
	$post=get_postdata();
	$setting=$this->plugin_setting('setting');
	$distance=ceil($post['distance']/1000);
	$price=$setting['start'];
	if($distance>$setting['juli']){
		$num=ceil($distance/$setting['juli']);
		$price+=($num-1)*$setting['price1'];
	}
	$res=array(
		'price'=>$price,
	);
	exi($res);
}
if($_RQ['op']=='create'){
	$post=get_postdata();
	$data=array_fields(array('start_seat','lat','end_lat','dest_seat','juli','time','gocity'),$post);
	if(!$data['dest_seat']){
		exi('请选择目的地','error');
	}
	if(!$data['dest_seat']){
		exi('未获取到距离和价格,请刷新重试','error');
	}
	$lat_lng=$this->session('lat_lng');
	$drivers=$this->model('worker')->getworkers($lat_lng);
	if(!$drivers[0]){
		exi('暂无司机,等待司机接单','error');
	}
	$isorder=pdo_get('jy_daijia_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1,6,7,8)));
	if($isorder['id']){
		exi('您还有未完成的订单，请先完成','error','jy_daijia/order/');
	}
	
	$data['lat']=ijson_encode($post['lat']);
	$data['end_lat']=ijson_encode($post['end_lat']);
	$data['mid']=$_SESSION['uid'];
	if($post['name']&&$post['mobile']){
		$data['name']=$post['name'];	
		$data['mobile']=$post['mobile'];	
	}else{
		$member=pdo_get('jy_daijia_member',array('mid'=>$_SESSION['uid']));
		$data['name']=$member['name'];	
		$data['mobile']=$member['mobile'];
	}
	$fees=$this->model('order')->get_fee($data['juli'],0,$post['gocity']);
	$data=array_merge($data,$fees);
	$orderid=$this->model('order')->create($data);
	$data['id']=$orderid;
	$this->model('worker')->syspai($data);
	$return['orderid']=$orderid;
	$return['msg']='已呼叫，请耐心等待司机接单';
	exi($return);
	
}
if($_RQ['op']=='history'){
	$post=get_postdata();
	$address=pdo_get('jy_daijia_history',array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'address'=>$post['address']));
	if($address){
		pdo_update('jy_daijia_history',array('createtime'=>TIMESTAMP),array('id'=>$address['id']));
	}else{
		if(!$post['pcd']){
			$post['pcd']=$post['province'].' '.$post['city'].' '.$post['district'];
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP,
			'address'=>$post['address'],
			'city'=>$post['city'],
			'pcd'=>$post['pcd'],
			'lat_lng'=>ijson_encode($post['lat_lng']),
			'title'=>$post['title']
		);
		pdo_insert('jy_daijia_history',$data);
	}
	$params=array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
    $orderby='createtime desc';
	$total='';
	$history=pdo_getslice('jy_daijia_history',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($history as &$v){
		$v['lat_lng']=json_decode($v['lat_lng'],true);
	}
	$return['list']=$history;
	exi($return);
}
if($_RQ['op']=='getworkers'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$list=$this->model('worker')->getworkers($lat_lng,$_RQ['maxjuli'],$_RQ['limit'],$_RQ['page']);
	$markers=array();
	foreach($list as $k=>&$item){
		$name=cfc('tools')->substr($item['name'],0,1);
		$name .='师傅';
		$markers[]=array(
			'nowjuli'=>$item['nowjuli'],
			'latitude'=>$item['latlng']['lat'],
			'longitude'=>$item['latlng']['lng'],
			'title'=>$name,
			'iconPath'=>$return['pluginset']['dai_icon']?tomedia($return['pluginset']['dai_icon']):tomedia('../addons/jy_daijia/resource/images/dai_icon.png'),
			'width'=>26,
			'height'=>26,
			'callout'=>array(
				'content'=>$name,
				'borderRadius'=>5,
				'bgColor'=>'#333',
				'color'=>'#fff',
				'textAlign'=>'center',
				'padding'=>5,
				'anchorY'=>0,
				'borderColor'=>'#333',
			),
		);
	}
	$return['list']=$markers;
	exi($return);
}
if($_RQ['op']=='get_isorder'){
	$isorder=pdo_get('jy_daijia_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1,6,7,8)));
	$isorder=$isorder?:array('error'=>1);
	exi($return);
}
if($_RQ['op']=='setonline'){
	$post=get_postdata();
	$id=$post['id'];
	$busy=$post['busy'];
	$update=array('busy'=>$busy);
	if($busy==1){
		$update['lastontime']=TIMESTAMP;
	}
	pdo_update('jy_daijia_driver',$update,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($busy==0){
		$this->model('worker')->up_dayonlinetime();
		exi('成功离线！');	
	}elseif($busy==1){
		exi('成功上线！');	
	}elseif($busy==2){
		exi('成功忙碌！');	
	}	 
}
if($_RQ['op']=='set_xieyi'){
	pdo_update('jy_daijia_member',array('is_xieyi'=>1),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('你已同意本协议！');	
}
if($_RQ['op']=='get_fee'){
	$post=get_postdata();
	$juli=$post['juli'];
	$fees=$this->model('order')->get_fee($juli,0,$post['gocity']);
	exi($fees);	
}	
?>